#!/usr/bin/env bash

# echo -n  "/pubx/llvm15-build-debug/bin/clang  -cc1 -ast-dump" > cmd_file_clang_debug_ast_dump
# echo -n  "/pubx/llvm15-build-debug/bin/clang  -c" > cmd_file_clang_debug_compile
# echo -n  "/usr/lib/llvm-14/bin/clang  -cc1 -ast-dump" > cmd_file_clang_sys_ast_dump
# echo -n  "/usr/lib/llvm-14/bin/clang  -c" > cmd_file_clang_sys_compile

#bash -x me.sh  cmd_file_clang_debug_ast_dump  cmd_file_clang_debugg_ast_dump  main1.cpp main2.cpp
#me.sh   cmd_file_clang_debug_ast_dump  cmd_file_clang_debug_compile  main1.cpp  main1_copy.cpp 


CXXCompilerCmdFile1=$1
CXXCompilerCmdFile2=$2
cxxSourceFile1FullPath=$3
cxxSourceFile2FullPath=$4

# CXXCompilerCmdFile1=cmd_file_clang_debug_ast_dump
CXXCompilerCmdText1=`cat $CXXCompilerCmdFile1`
echo $CXXCompilerCmdText1
#"/pubx/llvm15-build-debug/bin/clang  -cc1 -ast-dump

# CXXCompilerCmdFile2=cmd_file_clang_debug_ast_dump
CXXCompilerCmdText2=`cat $CXXCompilerCmdFile2`
echo $CXXCompilerCmdText2

# cxxSourceFile1FullPath=/pubx/llvm-project/analyze_clang_by_valgrind/main1.cpp
# cxxSourceFile2FullPath=/pubx/llvm-project/analyze_clang_by_valgrind/main2.cpp
usageText=" usage: me.sh <C++ Compiler command 1 file full path>  h <C++ Compiler command 2 file full path> <c++ source file 1 full path>   <c++ source file 2 full path>"
{ test "__$CXXCompilerCmdFile1" = "__" || test "__$CXXCompilerCmdText1" = "__" || test "__$CXXCompilerCmdFile2" = "__" || test "__$CXXCompilerCmdText2" = "__" || test "__$cxxSourceFile1FullPath" = "__" || test "__$cxxSourceFile2FullPath" = "__" ; } && { echo $usageText;  exit 5; }


#取带扩展名的命令文件名
#按/分隔, 取右边不含分隔符的最长部分
CmdFile1Name=${CXXCompilerCmdFile1##*/}
CmdFile2Name=${CXXCompilerCmdFile2##*/}
echo "CmdFile1Name=$CmdFile1Name,CmdFile2Name=$CmdFile1Name"
# CmdFile1Name=cmd_file_clang_release_ast_dump,CmdFile2Name=cmd_file_clang_release_ast_dump

#取带扩展名的文件名
#按/分隔, 取右边不含分隔符的最长部分
cxxSourceFile1Name=${cxxSourceFile1FullPath##*/}
cxxSourceFile2Name=${cxxSourceFile2FullPath##*/}
echo "cxxSourceFile1Name=$cxxSourceFile1Name,cxxSourceFile2Name=$cxxSourceFile2Name"
# cxxSourceFile1Name=main1.cpp,cxxSourceFile2Name=main2.cpp

#取不带扩展名的文件名
#按.分隔, 取左边不含分隔符的最长部分
cxx1Name=${cxxSourceFile1Name%.*}
cxx2Name=${cxxSourceFile2Name%.*}
echo "cxx1Name=$cxx1Name,cxx2Name=$cxx2Name"
# cxx1Name=main1,cxx2Name=main2

${CXXCompilerCmdText1} ${cxxSourceFile1FullPath} > /dev/null || { echo "直接运行1失败，退出，退出码为6"; exit 6; }
${CXXCompilerCmdText2} ${cxxSourceFile2FullPath} > /dev/null || { echo "直接运行2失败，退出，退出码为7"; exit 7; }

echo "--------------------------------------------------------------------------------------------------------------"

outHome="/tmp/`date +%s`"
#outHome=/tmp/1682555330
mkdir -p ${outHome}

f1_callgrind_out=${outHome}/${CmdFile1Name}_${cxx1Name}.callgrind_out
f2_callgrind_out=${outHome}/${CmdFile2Name}_${cxx2Name}.callgrind_out

f1_callgrind_annotate_out0=${outHome}/${CmdFile1Name}_${cxx1Name}.callgrind_annotate_out0
f2_callgrind_annotate_out0=${outHome}/${CmdFile2Name}_${cxx2Name}.callgrind_annotate_out0

f1_callgrind_annotate_outA=${outHome}/${CmdFile1Name}_${cxx1Name}.callgrind_annotate_outA
f2_callgrind_annotate_outA=${outHome}/${CmdFile2Name}_${cxx2Name}.callgrind_annotate_outA

f1_callgrind_annotate_outB=${outHome}/${CmdFile1Name}_${cxx1Name}.callgrind_annotate_outB
f2_callgrind_annotate_outB=${outHome}/${CmdFile2Name}_${cxx2Name}.callgrind_annotate_outB

f1_callgrind_annotate_outC=${outHome}/${CmdFile1Name}_${cxx1Name}.callgrind_annotate_outC
f2_callgrind_annotate_outC=${outHome}/${CmdFile2Name}_${cxx2Name}.callgrind_annotate_outC

#valgrind
valgrind  --trace-children=yes --tool=callgrind --callgrind-out-file=${f1_callgrind_out}  ${CXXCompilerCmdText1} ${cxxSourceFile1FullPath}
valgrind  --trace-children=yes --tool=callgrind --callgrind-out-file=${f2_callgrind_out}  ${CXXCompilerCmdText2} ${cxxSourceFile2FullPath}

# callgrind_annotate
callgrind_annotate --show-percs=no --auto=no  ${f1_callgrind_out}  > ${f1_callgrind_annotate_out0}
callgrind_annotate --show-percs=no --auto=no  ${f2_callgrind_out}  > ${f2_callgrind_annotate_out0}

#去掉前面多余的标题行
tail -n +26 ${f1_callgrind_annotate_out0} > ${f1_callgrind_annotate_outA}
tail -n +26 ${f2_callgrind_annotate_out0} > ${f2_callgrind_annotate_outA}

#列分割符  ： 从 两个空格  替换为 分号 
sed -E 's/([0-9]+)  /\1;/g' ${f1_callgrind_annotate_outA} > ${f1_callgrind_annotate_outB}
sed -E 's/([0-9]+)  /\1;/g' ${f2_callgrind_annotate_outA} > ${f2_callgrind_annotate_outB}

#按第2列 函数名排序
sort -t$';' -k2,2 ${f1_callgrind_annotate_outB} > ${f1_callgrind_annotate_outC}
sort -t$';' -k2,2 ${f2_callgrind_annotate_outB} > ${f2_callgrind_annotate_outC}

#以在该方法中执行的指令数目 间接的 比较方法调用次数
bcompare ${f1_callgrind_annotate_outC} ${f2_callgrind_annotate_outC}